Option Strict Off
Option Explicit On
Namespace BMLERP
	Friend Class frmMenuPad
		Inherits System.Windows.Forms.Form
		Dim OldPadcode As String
		Private Sub cboMenuPad_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboMenuPad.ClickEvent
			If eventArgs.area = 2 Then
				txtPadcode.Text = cboMenuPad.BoundText

				ISADDING = False
				ISEDITING = False
				WriteToScreen()
				' Ham Gan tri man hinh
				ButtonStatus()

			End If
		End Sub

		Private Sub cboParentPadCode_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboParentPadCode.ClickEvent
			Dim strFull As String
			If eventArgs.area = 2 Then
				txtParentPadcode.Text = cboParentPadCode.BoundText
				If txtParentPadcode.Text = "" Then
					MsgBox("Note ! This is a Menu Bar.")
					txtPadName.Text = "mn"
					txtFullPadName.Text = txtPadcode.Text
				Else
					strFull = Find("MenuPad", "fullpadname", " WHERE Padcode='" & txtParentPadcode.Text & "'")
					If strFull <> "" Then
						txtFullPadName.Text = strFull & "_" & txtPadcode.Text
					Else
						MsgBox("Not have this Pad code!")
						txtParentPadcode.Text = ""
						cboParentPadCode.Focus()
					End If
					txtPadName.Text = txtParentPadcode.Text
				End If
				txtPadIndex.Focus()
			End If
		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddnew.Click
			ISADDING = True
			ISEDITING = True
			WriteToScreen()
			' Ham Gan tri man hinh
			ButtonStatus()
			txtPadcode.Focus()
		End Sub

		Sub ButtonStatus()
			cmdAddnew.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING	' And txtIMITN <> ""
			cmdDelete.Enabled = Not ISEDITING 'And txtIMITN <> ""
			cmdCancel.Enabled = ISEDITING
			FramePadName.Enabled = ISEDITING
		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click

			WriteToScreen()
			ISADDING = False
			ISEDITING = False
			ButtonStatus()
			'txtCaption.SetFocus
		End Sub

		Private Sub cmdDelete_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDelete.Click
			Dim Response As Object
			Response = MsgBox("Do you really to delte this MenuPad?", MsgBoxStyle.YesNo + MsgBoxStyle.Critical + MsgBoxStyle.DefaultButton2, "Chu y'")
			If Response = MsgBoxResult.Yes Then DBconn.Execute("DELETE From MenuPad Where PadCode=" & txtPadcode.Text)
		End Sub

		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			ISEDITING = True
			WriteToScreen()
			' Ham Gan tri man hinh
			ButtonStatus()
			txtCaption.Focus()
		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click

			WriteToFile()
			cmdAddnew.Focus()
			'txtCaption.SetFocus
		End Sub

		Private Sub frmMenuPad_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc1.RecordSource = "Select PadCode,PadCode+' - '+ Caption  as PadNameIndex from MenuPad order by padcode"
			Adodc1.Refresh()

			ISADDING = False
			ISEDITING = False
			ButtonStatus()
			Me.VB6_AddADODataBinding()
			Me.cboMenuPad.RowSource = ADOBind_Adodc1.DataSource
			Me.cboParentPadCode.RowSource = ADOBind_Adodc1.DataSource
		End Sub
		Sub WriteToScreen()
			Dim RecMenuPad As ADODB.Recordset
			If ISADDING Then
				txtPadcode.Text = ""
				txtCaption.Text = ""
				txtPadName.Text = ""
				txtPadIndex.Text = CStr(0)
				txtParentPadcode.Text = ""

				txtFullPadName.Text = ""
			Else
				RecMenuPad = DBconn.Execute("Select * from MenuPad where PadCode='" & txtPadcode.Text & "'")
				If Not RecMenuPad.EOF Then
					txtCaption.Text = RecMenuPad.Fields("Caption").Value
					txtPadName.Text = RecMenuPad.Fields("PadName").Value
					txtPadIndex.Text = RecMenuPad.Fields("PadIndex").Value
					txtParentPadcode.Text = RecMenuPad.Fields("ParentPadCode").Value

					txtFullPadName.Text = RecMenuPad.Fields("FullPadName").Value
				Else
					txtPadcode.Text = ""
					txtCaption.Text = ""
					txtPadName.Text = ""
					txtPadIndex.Text = CStr(0)
					txtParentPadcode.Text = ""
					txtFullPadName.Text = ""
				End If
			End If
			cboMenuPad.BoundText = txtPadcode.Text
			cboParentPadCode.BoundText = txtParentPadcode.Text
			OldPadcode = txtPadcode.Text
		End Sub
		Sub WriteToFile()
			Dim SQL As Object
			If txtPadcode.Text = "" Then
				MsgBox("Invalid Pad code!")
				Exit Sub
			End If
			If ValidMaso(OldPadcode, txtPadcode, "MenuPad", " Padcode='" & txtPadcode.Text & "'") Then
				MsgBox("Duplicate Pad code!")
				Exit Sub
			End If

			If ISADDING Then
				SQL = "Insert into Menupad(Padcode,PadName,PadIndex,ParentPadcode,FullPadName,Caption) values " & "('" & txtPadcode.Text & "','" & txtPadName.Text & "'," & txtPadIndex.Text & ",'" & txtParentPadcode.Text & "'" & ",'" & txtFullPadName.Text & "','" & txtCaption.Text & "')"
			Else
				SQL = "Update MenuPad set  Padcode= '" & txtPadcode.Text & "', Padname= '" & txtPadName.Text & "',PadIndex=" & txtPadIndex.Text & ",ParentPadCode='" & txtParentPadcode.Text & "'" & ",FullPadName='" & txtFullPadName.Text & "',Caption='" & txtCaption.Text & "'" & " WHERE PadCode='" & txtPadcode.Text & "'"
			End If
			DBconn.Execute(SQL)

			Adodc1.RecordSource = "Select PadCode,PadCode+' - '+Caption   as PadNameIndex from MenuPad order by padcode"
			Adodc1.Refresh()
			cboMenuPad.ReFill()
			'cboMenuPad.CtlRefresh()
			cboMenuPad.Refresh()
			cboMenuPad.BoundText = txtPadcode.Text
			cboParentPadCode.ReFill()
			'cboParentPadCode.CtlRefresh()
			cboParentPadCode.Refresh()

			ISADDING = False
			ISEDITING = False
			ButtonStatus()
		End Sub



		Private Sub txtCaption_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtCaption.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtParentPadcode.Focus()

			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtPadcode_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtPadcode.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				If ValidMaso(OldPadcode, txtPadcode, "MenuPad", " Padcode='" & txtPadcode.Text & "'") Then
					MsgBox("Duplicate Pad code!")
					txtPadcode.Focus()
					GoTo EventExitSub
				End If
				txtCaption.Focus()
			End If
EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtPadIndex_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtPadIndex.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then cmdSave.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtPadName_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtPadName.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtPadIndex.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtParentIndex_KeyPress(ByRef KeyAscii As Short)
			Dim txtParentIndex As Object
			Dim txtParentName As Object
			Dim strFull As String
			If KeyAscii = 13 Then
				strFull = Find("MenuPad", "fullpadname", " WHERE Padname='" & txtParentName & "' and PadIndex=" & txtParentIndex)
				If strFull <> txtPadName.Text Then
					txtFullPadName.Text = strFull & "_" & txtPadName.Text
				Else
					txtFullPadName.Text = txtPadName.Text
				End If
				cmdSave.Focus()
			End If
		End Sub

		Private Sub txtParentName_KeyPress(ByRef KeyAscii As Short)
			If KeyAscii = 13 Then

			End If
		End Sub

		Private Sub txtParentPadcode_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtParentPadcode.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim strFull As String
			If KeyAscii = 13 Then
				If txtParentPadcode.Text = "" Then
					MsgBox("Note ! This is a Menu Bar.")
					txtPadName.Text = "mn"
					txtFullPadName.Text = txtPadcode.Text
				Else
					strFull = Find("MenuPad", "fullpadname", " WHERE Padcode='" & txtParentPadcode.Text & "'")
					If strFull <> "" Then
						txtFullPadName.Text = strFull & "_" & txtPadcode.Text
					Else
						MsgBox("Not have this Pad code!")
						txtParentPadcode.Text = ""
						cboParentPadCode.Focus()
					End If
					txtPadName.Text = txtParentPadcode.Text
				End If
				txtPadIndex.Focus()
			End If
			cboParentPadCode.BoundText = txtParentPadcode.Text
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
	End Class
End Namespace